/**
 *  @datetime 2019/2/15 9:42 AM
 *  @author Ping Dong
 *  @desc 网络请求
 */
 import { notification } from 'antd';
import { xhrGet as xhrGets,  xhrBlob as xhrBlobs, xhrPostUrlEncoding as xhrPostUrlEncodings,xhrDelete as xhrDeletes, xhrPostJson as xhrPostJsons, xhrFormData as xhrFormDatas} from '@ey-utils/utils/services';
 import serverApi from './api';
 // import
 const getUrl= (url:any)=>{
     return url;
  //  return `http://10.149.6.152${url.startsWith('/') ? '' : '/'}${url}`;
 }
 export function xhrGet(url: string, params?: any, headers?: any) {
  return new Promise((resolve, reject) => {
    xhrGets(getUrl(url), params, headers).then((res:any)=>{
      if (!res.success) {
        notification.error({ message: `${res.message}` });
      }
      resolve(res);
    }).catch((err:any)=>{
      notification.error({ message: `${err.message||'查询失败'}` });
      // reject(err);
    })
 })
 }

 export function xhrDelete(url: string, params?: any, headers?: any) {
  return new Promise((resolve, reject) => {
    xhrDeletes(getUrl(url),params,headers).then((res:any)=>{
      if (!res.success) {
        notification.error({ message: `${res.message}` });
      }
      resolve(res);
    }).catch((err:any)=>{
      notification.error({ message: `${err.message||'查询失败'}` });
      // reject(err);
    })
 })
 }

 export function xhrPostJson(url: string, params?: any, headers?: any) {
   return new Promise((resolve, reject) => {

      xhrPostJsons(getUrl(url),params,headers).then((res:any)=>{
        if (!res.success) {
          notification.error({ message: `${res.message}` });
        }
        resolve(res);
      }).catch((err:any)=>{
        notification.error({ message: `${err.message||'查询失败'}` });
        reject(err);
      })
   })
 }

 export function xhrPostUrlEncoding(url: string, params?: any, headers?: any) {
     return  xhrPostUrlEncodings(getUrl(url), params,headers)
 }

 export function xhrFormData(url: string, params?: any, headers?: any) {
   return xhrFormDatas(getUrl(url), params, headers)
 }

 export function xhrBlob(url: string, params?: any, headers?: any) {
   return xhrBlobs(getUrl(url), params, headers)
 }
 export const api = serverApi;


 // @ts-ignore
 export const isIdentifyFile = function (t: string) {
    return isIdentifyFile(t)
 };


 export const request = (url: any, body: any = {}) => {
  const { method, data, params } = body;
  switch (method) {
    case 'post':
    case 'POST':
      return xhrPostJson(url, data || params);
    case 'get':
    case 'GET':
    default:
      return xhrGet(url, params);
  }
};
